package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.rebate.UserRebateInfo;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserRebateDaoImpl extends TempDataObjectDao<UserRebateInfo> {

    public Map<String,UserRebateInfo> getUserRebateInfoMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String,UserRebateInfo> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_s_rebate";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserRebateInfo data = parse(rs);
                dataMap.put(data.getPlatformUserId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public UserRebateInfo getUserRebateInfo(String platformUserId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_rebate where platformUserId=?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setString(1, platformUserId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserRebateInfo data) {
        PreparedStatement call = null;
        String sql = "insert into t_s_rebate(platformUserId,vipExp,recharge,isGet,getTime,serverId,userId,nickName) values(?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setString(i++, data.getPlatformUserId());
            call.setLong(i++, data.getVipExp());
            call.setLong(i++, data.getRecharge());
            call.setBoolean(i++, data.isGet());
            call.setLong(i++, data.getGetTime());
            call.setLong(i++, data.getServerId());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getNickName());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserRebateInfo data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_s_rebate SET vipExp=?,recharge=?,isGet=?,getTime=?,serverId=?,userId=?,nickName=? where platformUserId=?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, data.getVipExp());
            call.setLong(i++, data.getRecharge());
            call.setBoolean(i++, data.isGet());
            call.setLong(i++, data.getGetTime());
            call.setLong(i++, data.getServerId());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getNickName());
            call.setString(i++, data.getPlatformUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserRebateInfo parseData(ResultSet rs) throws SQLException {
        UserRebateInfo data = new UserRebateInfo();
        data.setPlatformUserId(rs.getString("platformUserId"));
        data.setVipExp(rs.getLong("vipExp"));
        data.setRecharge(rs.getLong("recharge"));
        data.setGet(rs.getBoolean("isGet"));
        data.setGetTime(rs.getLong("getTime"));
        data.setServerId(rs.getLong("serverId"));
        data.setUserId(rs.getLong("userId"));
        data.setNickName(rs.getString("nickName"));
        return data;
    }
}
